package com.baoyouqun.service.impl;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baoyouqun.base.utils.ConvertUtils;
import com.baoyouqun.domain.CreateCmd.VirtualNumberCreateCmd;
import com.baoyouqun.domain.Query.VirtualNumberQuery;
import com.baoyouqun.domain.UpdateCmd.VirtualNumberUpdateCmd;
import com.baoyouqun.domain.VO.UserVO;
import com.baoyouqun.domain.VO.VirtualCallDTO;
import com.baoyouqun.domain.VO.VirtualNumberVO;
import com.baoyouqun.entity.VirtualNumber;
import com.baoyouqun.mapper.VirtualNumberMapper;
import com.baoyouqun.service.SystemConfigService;
import com.baoyouqun.service.VirtualNumberService;
import jakarta.annotation.Resource;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
public class VirtualNumberServiceImpl extends ServiceImpl<VirtualNumberMapper, VirtualNumber> implements VirtualNumberService {

    @Resource
    private VirtualNumberMapper virtualNumberMapper;
    @Autowired
    private UserServiceImpl userServiceImpl;
    @Autowired
    private SystemConfigService systemConfigService;

    @Override
    public SingleResponse<VirtualNumberVO> create(VirtualNumberCreateCmd cmd) {
        VirtualNumber entity = ConvertUtils.copyBean(cmd, VirtualNumber.class);
        Boolean result = save(entity);
        if (!result) {
            return SingleResponse.buildFailure("100101", "创建失败");
        }

        VirtualNumberVO vo = ConvertUtils.copyBean(entity, VirtualNumberVO.class);
        return SingleResponse.of(vo);
    }

    @Override
    public Response update(VirtualNumberUpdateCmd cmd) {
        if (Objects.isNull(cmd)) {
            return Response.buildFailure("100301", "参数不能为空");
        }
        if (StringUtil.isBlank(cmd.getId())) {
            return Response.buildFailure("100302", "ID不能为空");
        }
        VirtualNumber oldEntity = getById(cmd.getId());
        if (Objects.isNull(oldEntity)) {
            return Response.buildFailure("100303", "未查询到此数据");
        }
        oldEntity = ConvertUtils.copyBean(cmd, VirtualNumber.class);
        Boolean result = updateById(oldEntity);
        if (result) {
            return Response.buildSuccess();
        } else {
            return Response.buildFailure("100304", "更新失败");
        }
    }

    @Override
    public VirtualNumberVO selectById(String id) {
        VirtualNumber entity = virtualNumberMapper.selectById(id);
        VirtualNumberVO vo =
                ConvertUtils.copyBean(entity, VirtualNumberVO.class);
        return vo;
    }

    @Override
    public IPage<VirtualNumberVO> pageVo(VirtualNumberQuery query) {
        IPage<VirtualNumber> page = new Page<VirtualNumber>(query.getCurrent(), query.getSize());
        page = page(page, new LambdaQueryWrapper<VirtualNumber>());
        Page<VirtualNumberVO> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<VirtualNumberVO> records = ConvertUtils.copyBeanList(page.getRecords(), VirtualNumberVO.class);
        voPage.setRecords(records);
        return voPage;
    }

    @Override
    public SingleResponse<VirtualNumberVO> getUserExtraPhonesInfo(String userId) {
        VirtualNumberVO virtualNumberVO = new VirtualNumberVO();
        virtualNumberVO.setUserId(userId);
        UserVO userVO = userServiceImpl.selectById(userId);
        virtualNumberVO.setPhoneNumber(userVO.getExtraPhones());

        virtualNumberVO.setVirtualNumber("未开通");
        virtualNumberVO.setExpireMinutes(0L);

        if (StringUtil.isNotBlank(userId)) {
            VirtualNumber entity = getOne(new LambdaQueryWrapper<VirtualNumber>().eq(VirtualNumber::getUserId, userId).last("limit 1"));
            if (entity != null) {
                virtualNumberVO = ConvertUtils.copyBean(entity, VirtualNumberVO.class);
            }
        }
        

        VirtualCallDTO virtualCall = systemConfigService.getVirtualCall();
        virtualNumberVO.setList(virtualCall.getList());
        virtualNumberVO.setPhoneNumber(userVO.getExtraPhones());
        return SingleResponse.of(virtualNumberVO);
    }
}